/**
 * @author yxw
 * date : 2013年10月16日 下午4:53:23 
 */
package com.tzinfo.tzt.activity;


import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.view.MyProgressDialog;
import com.tzinfo.tzt.config.AppDataConfig;
import com.tzinfo.tzt.domain.MyResultList;
import com.tzinfo.tzt.domain.NewhouseHouseDetail;
import com.tzinfo.tzt.util.HttpTools;
import com.tzinfo.tzt.util.JsonTools;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MoreParasActivity extends Activity implements OnClickListener{
	private MyResultList moreParasResult;
	private String sHouseId = "";
	private Dialog dialog;
	private NewhouseHouseDetail moreParasDetail;
	private Button backBtn;
	private TextView[] textView=new TextView[12];
	private String sAddress="";
	private String sSchool="";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_more_paras);
		sHouseId = this.getIntent().getStringExtra("houseId");
		sAddress = this.getIntent().getStringExtra("Address");
		sSchool = this.getIntent().getStringExtra("School");
		init();
	}

	private void init() {
		dialog = new MyProgressDialog(this).createDialog(getResources()
				.getString(R.string.loading));
		// 加载进度条
		dialog.show();
		backBtn = (Button) this.findViewById(R.id.bt_back);
		backBtn.setOnClickListener(this);
		
		textView[0]=(TextView)this.findViewById(R.id.text_OpenTime);
		textView[1]=(TextView)this.findViewById(R.id.text_GiveTime);
		textView[2]=(TextView)this.findViewById(R.id.text_Developer);
		textView[3]=(TextView)this.findViewById(R.id.text_ManageCompany);
		textView[4]=(TextView)this.findViewById(R.id.text_BuildType);
		textView[5]=(TextView)this.findViewById(R.id.text_Decorate);
		textView[6]=(TextView)this.findViewById(R.id.text_Area);
		textView[7]=(TextView)this.findViewById(R.id.text_BuildArea);
		textView[8]=(TextView)this.findViewById(R.id.text_Address);
		textView[9]=(TextView)this.findViewById(R.id.text_School);
		textView[10]=(TextView)this.findViewById(R.id.text_PlotRatio);
		textView[11]=(TextView)this.findViewById(R.id.text_GreeningRate);
		new Thread(getNewHouseMoreParasRun).start();
	}
	private void updateUI(){
		textView[0].setText(moreParasDetail.getOpenTime());
		textView[1].setText(moreParasDetail.getGiveTime());
		textView[2].setText(moreParasDetail.getDeveloper());
		textView[3].setText(moreParasDetail.getManageCompany());
		textView[4].setText(moreParasDetail.getBuildType());
		textView[5].setText(moreParasDetail.getDecorate());
		textView[6].setText(moreParasDetail.getArea());
		textView[7].setText(moreParasDetail.getBuildArea());
		textView[8].setText(sAddress);
		textView[9].setText(sSchool);
		textView[10].setText(moreParasDetail.getPlotRatio());
		textView[11].setText(moreParasDetail.getGreeningRate());
	}
	
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.arg1) {
			case 1:
				updateUI();
				dialog.dismiss();
				break;
			case -1:
				getDataError((String) msg.obj);
				break;
			case 5:
				break;
			case 7:
				break;
			case 0:
				break;
			}
		}
	};

	/**
	 * 线程 得到新房列表
	 */
	Runnable getNewHouseMoreParasRun = new Runnable() {

		@Override
		public void run() {
			Message msg = handler.obtainMessage();
			String jsonString;
			String url=AppDataConfig.xinfangUrl
					+ AppDataConfig.NewhouseHouseDetail + "?id=" + sHouseId;
			Log.e("url", url);
			jsonString = HttpTools.getJsonContent(url);
			Log.e("JsonString", jsonString);
			moreParasResult = JsonTools.getNewhouseHouseDetail(jsonString);
			getData();
			if (moreParasResult.getSuccess()) {
				msg.arg1 = 1;
			} else {
				msg.arg1 = -1;
				msg.obj = moreParasResult.getErrorMessage();
			}
			handler.sendMessage(msg);
		}
	};

	// 得到data数据
	private void getData() {
		if (moreParasResult.getSuccess()) {
			moreParasDetail = (NewhouseHouseDetail) moreParasResult.getData().get(0);
		}
	}
	private void getDataError(String ERRORMESSAGE) {
		new AlertDialog.Builder(this).setCancelable(false)
				.setMessage(ERRORMESSAGE)
				.setPositiveButton(getResources().getString

				(R.string.cancel), new DialogInterface.OnClickListener()

				{
					public void onClick

					(DialogInterface dialog, int which) {
						finish();
					}
				}).setNegativeButton(getResources().getString

				(R.string.try_again), new DialogInterface.OnClickListener()

				{
					public void onClick(

					DialogInterface dialogInterface, int

					whichButton) {
						dialog.show();
						new Thread(getNewHouseMoreParasRun).start();
					}
				}).show();
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_back:
			this.finish();
			break;

		default:
			break;
		}
		
	}
}
